package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.OverflowListService;
import com.atguigu.jxc.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2022/1/4 11:03
 */
@Service
public class OverflowListServiceImpl implements OverflowListService {

    @Autowired
    private OverflowListDao overflowListDao;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void save(OverflowList overflowList, HttpSession session, List<OverflowListGoods> readValue) {
        User user = UserUtil.getUser(session);

        overflowList.setUserId(user.getUserId());
        overflowList.setTrueName(user.getTrueName());
        overflowListDao.saveList(overflowList);
        Integer listId = overflowList.getOverflowListId();


        readValue.forEach(overflowListGoods -> overflowListGoods.setOverflowListId(listId));

        overflowListDao.saveListGoods(readValue);


    }

    @Override
    public Map<String, Object> getList(String sTime, String eTime) {
        HashMap<String, Object> result = new HashMap<>();
        List<DamageList> list = overflowListDao.getList(sTime,eTime);
        result.put("rows",list);


        return result;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer overflowListId) {
        HashMap<String, Object> result = new HashMap<>();
        List<DamageListGoods> list = overflowListDao.getGoodsListById(overflowListId);

        result.put("rows",list);

        return result;
    }
}
